define(function(require){
	var main = require('/static/main.js');
	var m = new main();
	var type = {
		'0' : {isfirst:true},
		'1' : {isfirst:true},
		'2' : {isfirst:true},
	}
	function Cont(){
		this.Fun._init();
	}
	Cont.prototype.Fun = {
		colorList:[
            '#fb4155',
            '#ff8f6e',
            '#ffe659',
            '#79c3fc',
            '#6be6c1',
            '#516b91',
            '#edafda',
            '#7a7a7a',
            '#51b7b7',
			'#338ee3',
			'#848baf',
			'#F3638B',
        ],
		_init:function(){
			this.bindEvent();
		},
		bindEvent:function(){
			var _this = this;
		    //选择项目
		    $(document).on("click",".nav-tabs li",function(){
		        var index = $(this).index();
		        _this.getdetialData(index);
		    });
		    this.getdetialData(0);
		},
		getStyleCss:function(index,data){
			var _this = this;
		   switch (parseInt(index)) {
	         case 0:
	            var chart1 = document.getElementById('echart1');
			    var echart1 = echarts.init(chart1,'customed');
			    var option1 = {
	          		color:_this.colorList,
			        tooltip : {
			            trigger: 'item'
			        },
			        legend: {
			            show:false
			        },

			        series : [
			            {
			                type: 'pie',
			                radius : '50%',
			                center: ['50%', '50%'],

			                data:data,
			                itemStyle: {
			                    emphasis: {
			                        shadowBlur: 10,
			                        shadowOffsetX: 0,
			                        shadowColor: 'rgba(0, 0, 0, 0.5)'
			                    }
			                }
			            }
			        ]
			    };
			    echart1.setOption(option1);
	           break;
	         case 1:
	         	console.log(data);
	         	var chart2 = document.getElementById('echart2');
		        var echart2= echarts.init(chart2,'customed');
			    var option2 = {
	          		color:_this.colorList,
			        tooltip: {
		            	trigger: 'axis'
			        },
			        xAxis: {
			            type: 'category',
			            boundaryGap: false,
			            data:data.dateList
			        },
			        yAxis: {
			            type: 'value',
			            minInterval:1
			        },
			        series: [
			            {
			                name:'新增客户',
			                type:'line',
			                markPoint: {
			                    data: [
			                        {type: 'max', name: '最大值'},
			                        {type: 'min', name: '最小值'}
			                    ]
			                },
			                data:data.dataList1
			            },
			        ]
			    };
		        echart2.setOption(option2);
	           break;
	          case 2 :
	          	var chart3 = document.getElementById('echart3');
			    var echart3 = echarts.init(chart3,'customed');
	          	var option3 = {
	          		color:_this.colorList,
			        tooltip : {
			            trigger: 'item'
			        },
			        legend: {
			            show:false
			        },
			        series : [
			            {
			                type: 'pie',
			                radius : ['50%','70%'],
			                avoidLabelOverlap: false,
			                center: ['50%', '50%'],
			                data:data,
			                label: {
			                    normal: {
			                        show: false,
			                        position: 'center'
			                    },
			                    emphasis: {
			                        show: true,
			                        textStyle: {
			                            fontSize: '20',
			                            fontWeight: 'bold'
			                        }
			                    }
			                },
			                itemStyle: {
			                    emphasis: {
			                        shadowBlur: 10,
			                        shadowOffsetX: 0,
			                        shadowColor: 'rgba(0, 0, 0, 0.5)'
			                    }
			                }
			            }
			        ]
			    };
			    echart3.setOption(option3);
	       }
		},
		getdetialData:function(index){
			if(!type[index].isfirst){
				return false;
			}
			var _this = this;
			var param = {};
			param.type = index;
			param.Range = 30;
			$.ajax({
                type: "POST",
                url: "/analysis/customer-source",
                data: param,  
                dataType: "json",
                success: function (ret) {
                  if(ret.IsBizSuccess){
	                  // 来源
	                  if(index == 0){
	                  	var data = [];
	                  	var str = ''
	                  	$.each(ret.Models,function(k,v){
	                  		var SourceName = '';
	                  		switch (v.Source){
			                    case 0 : SourceName = '未知';break;
			                    case 100 : SourceName = '微信朋友圈';break;
			                    case 200 : SourceName = '微信好友';break;
			                    case 300 : SourceName = 'QQ好友';break;
			                    case 400 : SourceName = 'QQ空间';break;
			                    case 500 : SourceName = '新浪微博';break;
			                    case 600 : SourceName = '录入';break;
			                    case 700 : SourceName = '现场集客';break;
			                    case 800 : SourceName = '其他';break;
			                }
	                		var val = {
	                			value : v.Count,
	                			name : SourceName,
	                		}
	                		data.push(val);
	                		str +='<tr>\
			                        <td class="middle-align"><i class="fa fa-circle  m-r-15" style="color:'+_this.colorList[k]+'"></i>'+SourceName+'</td>\
			                        <td class="middle-align">'+v.Count+'</td>\
			                    </tr>';
	                	})
                          if(data.length < 1){
                              var val = {
                                  value : 0,
                                  name : '暂无客户',
                              }
                              data.push(val);
                              str +='<tr>\
			                        <td class="middle-align"><i class="fa fa-circle  m-r-15" style="color:'+_this.colorList[0]+'"></i>暂无客户</td>\
			                        <td class="middle-align">0</td>\
			                    </tr>';
                          }
	                	$('.comeWhere tbody').html(str);
	                	//  那几个 数字
	                	var UserCnt = ret.ToDayAdd ? ret.ToDayAdd : 0;
	                	var FansCnt = ret.WeekAdd ? ret.WeekAdd : 0;
	                	var VIPCnt = ret.MonthAdd ? ret.MonthAdd : 0;
	                	var CustomerCnt = ret.CustomerCnt ? ret.CustomerCnt : 0;
	                	$('.counter.fans').html(UserCnt);
	                	$('.counter.user').html(FansCnt);
	                	$('.counter.vip').html(VIPCnt);
	                	$('.counter.customers').html(CustomerCnt);
	                	jQuery(document).ready(function($) {
					        $('.counter').counterUp({
					            delay: 100,
					            time: 1200
					        });
					    });
	                  }else if(index == 1){
	                  	// 增长
	                  	var datalist = [ret.CustomerRise,ret.FansRise,ret.VIPRise]
	                  	// console.log(datalist);
	                  	var data = _this.getList(30,datalist);
	                  }else if(index == 2){
	                  	// 标签
	                  	var data = [];
	                  	var str = ''
	                  	$.each(ret.Models,function(k,v){
	                  		if(v.TagName == null){
	                  			v.TagName = '暂无标签'
	                  		}
	                		var val = {
	                			value : v.Count,
	                			name : v.TagName,
	                		}
	                		data.push(val);
	                		str +='<tr>\
			                        <td class="middle-align"><i class="fa fa-circle  m-r-15" style="color:'+_this.colorList[k]+'"></i>'+v.TagName+'</td>\
			                        <td class="middle-align">'+v.Count+'</td>\
			                        <td class="middle-align">'+((v.Count/ret.CustomerCnt)*100).toFixed(2)+'</td>\
			                    </tr>'
	                	})
                          if(data.length < 1){
                              var val = {
                                  value :0,
                                  name : '暂无标签',
                              }
                              data.push(val);
                              str +='<tr>\
			                        <td class="middle-align"><i class="fa fa-circle  m-r-15" style="color:'+_this.colorList[0]+'"></i>暂无标签</td>\
			                        <td class="middle-align">0</td>\
			                        <td class="middle-align">0</td>\
			                    </tr>'
                          }
	                	// console.log(_this.colorList);
	                	$('.signCont tbody').html(str);
	                  }
	                  type[index].isfirst = false;
                  }
                  _this.getStyleCss(index,data);
                }
            });
		},
		getList:function(count,data){

            var dateList = [];
            var dataList0 = [];
            var dataList1 = [];
            var dataList2 = [];
            for(var i = 0; i< count; i++){
                var date = new Date(new Date()- 86400000*i );
                var Year = date.getFullYear();
                var Month = date.getMonth()+1;
                var Month1 = Month < 10 ? '0'+ Month :Month;
                var Day = date.getDate();
                Day = Day < 10 ? '0'+ Day :Day;
                dateList.splice(0, 0, Month1+'-'+Day);

                var TimeStr = Year+'-'+Month1+'-'+Day;
                var timestamp = Date.parse(TimeStr)/100000;
                var Count0 = 0;
                var Count1 = 0;
                var Count2 = 0;

                for (var l = 0; l <= 2; l++) {
                	$.each(data[l],function(k,v){
	                    var TimeStr2 = m.formatYmd1(v.CreateTime);
	                    var timestamp1 = Date.parse(TimeStr2)/100000;
	                    if(timestamp == timestamp1){
	                        if(typeof (data[l][k]) != 'undefined' && data[l][k].Count){
	                        	var ret = data[l][k].Count;
	                        	if(l == 0){
	                        		Count0 = ret;
	                        	}else if(l ==1){
	                        		Count1 = ret;
	                        	}else{
	                        		Count2 = ret;
	                        	}
	                        }
	                    }
	                });
                }
                dataList0.splice(0, 0, Count0);
	            dataList1.splice(0, 0, Count1);
	            dataList2.splice(0, 0, Count2);

            }
            var JsonData = {
                dateList:dateList,
                dataList0:dataList0,
                dataList1:dataList1,
                dataList2:dataList2,
            }
           return JsonData;
        }

		


	}
	var a = new Cont();

})